clear all
set more off
cap log close

do "H:\Lavecchia_7086\to-transfer-jan-2022\RESTAT_Replication_Programs\0_Set_Directories.do"


****************************************************************************
* This do-file uses capital_income_final 
*	- capital gains, income, dividends and rrsp related variables
* 
* Prepares graphs
* 
****************************************************************************

*log using "$dir_log/3b_Graphs.log", replace


***** Graphs by year all individuals *******

clear

set obs 300
gen id=_n
gen intinc=_n*1000
gen province=.
gen year=.
save  "$dir_data/TaxCalculations/baseline.dta", replace

* calculate the first tax liability

clear
forvalues year =1991(1)2016{
forvalues province=1(1)13{
use "$dir_data/TaxCalculations/baseline.dta"
replace year=`year'
replace province=`province'
save "$dir_data/TaxCalculations/MTR1_`year'_`province'.dta", replace
*
global ctacs "$dir_do/CTaCS-2016-2/"  		
global inout "$dir_data//TaxCalculations"   
global ctacstemp "$dir_data/TaxCalculations"   
global logfile "interface1.log"                                   
global indata "MTR1_`year'_`province'"                                    
global outdata "MTR1_`year'_`province'" 
qui do "$ctacs/files/loadprogs.do"
ctacs
*
keep id year intinc fedincometax provincometax   
rename  fedincometax fedincometax1 
rename  provincometax provincometax1
save  "$dir_data/TaxCalculations/MTR1_`year'_`province'.dta", replace
clear
}
}

* calculate the second tax liability
forvalues year =1982(1)2016{
forvalues province=1(1)13{
use "$dir_data/TaxCalculations/baseline.dta"
replace year=`year'
replace province=`province'
replace intinc=intinc+100
save "$dir_data/TaxCalculations/MTR2_`year'_`province'.dta", replace

global ctacs "$dir_do/CTaCS-2016-2/"  		
global inout "$dir_data/TaxCalculations"   
global ctacstemp "$dir_data/TaxCalculations"   
global logfile "interface1.log"                                   
global indata "MTR2_`year'_`province'"                                   
global outdata "MTR2_`year'_`province'" 
qui do "$ctacs/files/loadprogs.do"
ctacs
keep id year province intinc fedincometax provincometax   
rename  fedincometax fedincometax2
rename  provincometax provincometax2
save  "$dir_data/TaxCalculations/MTR2_`year'_`province'.dta", replace
clear
}
}


* calculate MTRs:
forvalues year =1982(1)2016{
forvalues province=1(1)13{
use  "$dir_data/TaxCalculations/MTR2_`year'_`province'.dta"
merge 1:1 id year using "$dir_data/TaxCalculations/MTR1_`year'_`province'.dta"
drop _merge
gen mtr_fed=(fedincometax2-fedincometax1)/100
gen mtr_prov=(provincometax2-provincometax1)/100
save  "$dir_data/TaxCalculations/MTR_`year'_`province'.dta", replace
*
}
}

* combine all files
clear
forvalues year =1982(1)2016{
forvalues province=1(1)13{
append using  "$dir_data/TaxCalculations/MTR_`year'_`province'.dta"
}
}
keep year province intinc mtr_fed mtr_prov
replace intinc=intinc-100
save  "$dir_data/TaxCalculations/MTR_formerge.dta", replace

use  "$dir_data/TaxCalculations/MTR_formerge.dta"
gen temp1=mtr_fed if intinc== 299000
gen temp2=mtr_prov if intinc==299000
bysort year province: egen max_fed=max(mtr_fed)
bysort year province: egen max_prov=max(mtr_prov)
drop temp1 temp2

replace mtr_fed=0 if mtr_fed<0 /* I verified that this is correct */
replace mtr_prov=0 if mtr_prov<0 & (province==5 | province==13) /* I verified that this is correct */

replace mtr_prov=mtr_prov[_n-1] if province==1 & intinc==125000 & year>=2001
replace mtr_prov=mtr_prov[_n-1] if province==1 & intinc==175000 & year>=2001

save  "$dir_data/TaxCalculations/MTR_formerge.dta", replace

*erase "$dir_data/TaxCalculations/MTR1_`year'_`province'.dta"
*erase "$dir_data/TaxCalculations/MTR2_`year'_`province'.dta"



